package com.yunhai.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.UserHolderDto;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.common.utils.UserHolder;
import com.yunhai.model.vo.sys.SysUserFeedbackVo;
import com.yunhai.system.mapper.FeedbackMapper;
import com.yunhai.system.service.FeedbackService;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.model.entity.sys.Feedback;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Resource
    private FeedbackMapper feedbackMapper;

    public IPage getFeedbackPagination(PaginationVo paginationVo) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<Feedback> lqs = new LambdaQueryWrapper<>();
        lqs.orderByDesc(Feedback::getCreateTime);
        feedbackMapper.selectPage(page, lqs);

        return page;
    }

    @Override
    @Transactional
    public void addFeedBack(SysUserFeedbackVo sysUserFeedbackVo) throws Exception {

        UserHolderDto user = UserHolder.getUser();
        Feedback feedback = new Feedback();
        ReflectionCopy.copyProperties( feedback, sysUserFeedbackVo );
        feedback.setUserId(user.getId());
        feedbackMapper.insert(feedback);
        return;
    }

    @Override
    public void batchRemoveUserFeedback(List<Long> ids) throws Exception {
        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        feedbackMapper.deleteBatchIds(ids);
    }
}
